#include <ArduinoJson.h>
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "";//WIFI名称
const char* password = "";//WIFI密码
String city = "";//所在城市
String key = "";//心知天气密钥
String url = "https://api.seniverse.com/v3/weather/daily.json";
void Http_Init();

void setup() {
  Http_Init();
}

void loop() {
}

void Http_Init() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    //delay(500);
    //Serial.print(".");
  }
  //Serial.println("连接成功");

  HTTPClient http;                                                                     //创建类的实例
  http.begin(url + "?key=" + key + "&location=" + city + "&language=zh-Hans&unit=c");  //建立心知天气API获取近三日天气请求资源地址
  int http_code = http.GET();                                                          //HTTP状态码
  Serial.print(F("HTTP状态码:"));                                                      //200 ：请求成功。  404：请求的网页不存在。   500：服务器内部错误。
  Serial.println(http_code);                                                           //打印状态码
  String response = http.getString();                                                  //存储响应数据
  //Serial.print(F("响应数据："));                                           
  //Serial.println(response);                                                          //打印响应数据
  http.end();

  DynamicJsonDocument doc(2048);                          //分配了2048字节的内存
  deserializeJson(doc, response);                         //反序列化JSON数据：
  JsonObject results_0 = doc["results"][0];               //从doc中提取名为results的数组的第一个元素（索引为0），并将其存储在results_0对象中
  JsonObject results_0_location = results_0["location"];  //从results_0中提取名为location的对象，并将其存储在results_0_location对象中
  /********************************************************位置信息*****************************************************/
  const char* results_0_location_id = results_0_location["id"];                            // "WM6N2PM3WY2K"
  const char* results_0_location_name = results_0_location["name"];                        // "成都"
  const char* results_0_location_country = results_0_location["country"];                  // "CN"
  const char* results_0_location_path = results_0_location["path"];                        // "成都,成都,四川,中国"
  const char* results_0_location_timezone = results_0_location["timezone"];                // "Asia/Shanghai"
  const char* results_0_location_timezone_offset = results_0_location["timezone_offset"];  // "+08:00"

  String results_0_location_id_str = results_0_location["id"].as<String>();                            //位置ID
  String results_0_location_name_str = results_0_location["name"].as<String>();                        //地点名称
  String results_0_location_country_str = results_0_location["country"].as<String>();                  //国家名称
  String results_0_location_path_str = results_0_location["path"].as<String>();                        //位置名称
  String results_0_location_timezone_str = results_0_location["timezone"].as<String>();                //时区名称
  String results_0_location_timezone_offset_str = results_0_location["timezone_offset"].as<String>();  //时区偏移
  // Serial.println("位置ID:" + results_0_location_id_str);
  // Serial.println("地点名称:" + results_0_location_name_str);
  // Serial.println("国家名称:" + results_0_location_country_str);
  // Serial.println("位置名称:" + results_0_location_path_str);
  // Serial.println("时区名称:" + results_0_location_timezone_str);
  // Serial.println("时区偏移:" + results_0_location_timezone_offset_str);
  /********************************************************今日天气信息*****************************************************/
  //JsonObject results_0 = doc["results"][0];
  JsonArray results_0_daily = results_0["daily"];
  JsonObject results_0_daily_0 = results_0_daily[0];
  const char* results_0_daily_0_date = results_0_daily_0["date"];                                          //日期
  const char* results_0_daily_0_text_day = results_0_daily_0["text_day"];                                  //白天天气现象
  const char* results_0_daily_0_code_day = results_0_daily_0["code_day"];                                  //白天天气现象代码
  const char* results_0_daily_0_text_night = results_0_daily_0["text_night"];                              //夜晚天气现象
  const char* results_0_daily_0_code_night = results_0_daily_0["code_night"];                              //夜晚天气现象代码
  const char* results_0_daily_0_high = results_0_daily_0["high"];                                          //当天最高温度
  const char* results_0_daily_0_low = results_0_daily_0["low"];                                            //当天最低温度
  const char* results_0_daily_0_rainfall = results_0_daily_0["rainfall"];                                  //降水概率
  const char* results_0_daily_0_precip = results_0_daily_0["precip"];                                      //降水
  const char* results_0_daily_0_wind_direction = results_0_daily_0["wind_direction"];                      //风向
  const char* results_0_daily_0_wind_direction_degree = results_0_daily_0["wind_direction_degree"];        //风向角度值
  const char* results_0_daily_0_wind_speed = results_0_daily_0["wind_speed"];                              //风速
  const char* results_0_daily_0_wind_scale = results_0_daily_0["wind_scale"];                              //风力等级
  const char* results_0_daily_0_humidity = results_0_daily_0["humidity"];                                  //湿度
  // 通过串口监视器显示以上信息
  String results_0_daily_0_date_str = results_0_daily_0["date"].as<String>();                              //日期
  String results_0_daily_0_text_day_str = results_0_daily_0["text_day"].as<String>();                      //白天天气现象
  int results_0_daily_0_code_day_str = results_0_daily_0["code_day"].as<int>();                            //白天天气现象代码
  String results_0_daily_0_text_night_str = results_0_daily_0["text_night"].as<String>();                  //夜晚天气现象
  int results_0_daily_0_code_night_str = results_0_daily_0["code_night"].as<int>();                        //夜晚天气现象代码
  unsigned int results_0_daily_0_high_str = results_0_daily_0["high"].as<int>();                           //当天最高温度
  unsigned int results_0_daily_0_low_str = results_0_daily_0["low"].as<int>();                             //当天最低温度
  String results_0_daily_0_rainfall_str = results_0_daily_0["rainfall"].as<String>();                      //降水概率
  String results_0_daily_0_precip_str = results_0_daily_0["precip"].as<String>();                          //降水
  String results_0_daily_0_wind_direction_str = results_0_daily_0["wind_direction"].as<String>();          //风向
  int results_0_daily_0_wind_direction_degree_str = results_0_daily_0["wind_direction_degree"].as<int>();  //风向角度值
  String results_0_daily_0_wind_speed_str = results_0_daily_0["wind_speed"].as<String>();                  //风速
  int results_0_daily_0_wind_scale_str = results_0_daily_0["wind_scale"].as<int>();                        //风力等级
  int results_0_daily_0_humidity_str = results_0_daily_0["humidity"].as<int>();                            //湿度

  /*今日天气信息打印方式一*/
  // Serial.print(F("日期："));
  // Serial.println(results_0_daily_0_date_str);
  // Serial.print(F("白天天气现象："));
  // Serial.println(results_0_daily_0_text_day_str);
  // Serial.print(F("白天天气现象代码："));
  // Serial.println(results_0_daily_0_code_day_str);
  // Serial.print(F("夜晚天气现象："));
  // Serial.println(results_0_daily_0_text_night_str);
  // Serial.print(F("夜晚天气现象代码："));
  // Serial.println(results_0_daily_0_code_night_str);
  // Serial.print(F("当天最高温度："));
  // Serial.println(results_0_daily_0_high_str);
  // Serial.print(F("当天最低温度："));
  // Serial.println(results_0_daily_0_low_str);
  // Serial.print(F("降水概率："));
  // Serial.println(results_0_daily_0_rainfall_str);
  // Serial.print(F("降水："));
  // Serial.println(results_0_daily_0_precip_str);
  // Serial.print(F("风向："));
  // Serial.println(results_0_daily_0_wind_direction_str);
  // Serial.print(F("风向角度值："));
  // Serial.println(results_0_daily_0_wind_direction_degree_str);
  // Serial.print(F("风速："));
  // Serial.println(results_0_daily_0_wind_speed_str);
  // Serial.print(F("风力等级："));
  // Serial.println(results_0_daily_0_wind_scale_str);
  // Serial.print(F("湿度："));
  // Serial.print(results_0_daily_0_humidity_str);

  /*今日天气信息打印方式二*/
  // Serial.printf("日期：%s\n",results_0_daily_0_date_str);
  // Serial.printf("白天天气现象：%s\n",results_0_daily_0_text_day_str);
  // Serial.printf("白天天气现象代码：%d\n",results_0_daily_0_code_day_str);
  // Serial.printf("夜晚天气现象：%s\n",results_0_daily_0_text_night_str);
  // Serial.printf("夜晚天气现象代码：%d\n",results_0_daily_0_code_night_str);
  // Serial.printf("当天最高温度：%d\n",results_0_daily_0_high_str);
  // Serial.printf("当天最低温度：%d\n",results_0_daily_0_low_str);
  // Serial.printf("降水量：%s\n",results_0_daily_0_rainfall_str);
  // Serial.printf("降水概率：%s\n",results_0_daily_0_precip_str);
  // Serial.printf("风向：%s\n",results_0_daily_0_wind_direction_str);
  // Serial.printf("风向角度值：%s\n",results_0_daily_0_wind_direction_degree_str);
  // Serial.printf("风速：%s\n",results_0_daily_0_wind_speed_str);
  // Serial.printf("风力等级：%d\n",results_0_daily_0_wind_scale_str);
  // Serial.printf("湿度：%d\n",results_0_daily_0_humidity_str);

  /********************************************************明日天气信息*****************************************************/
  JsonObject results_0_daily_1 = results_0_daily[1];
  const char* results_0_daily_1_date = results_0_daily_1["date"];                                          //日期
  const char* results_0_daily_1_text_day = results_0_daily_1["text_day"];                                  //白天天气现象
  const char* results_0_daily_1_code_day = results_0_daily_1["code_day"];                                  //白天天气现象代码
  const char* results_0_daily_1_text_night = results_0_daily_1["text_night"];                              //夜晚天气现象
  const char* results_0_daily_1_code_night = results_0_daily_1["code_night"];                              //夜晚天气现象代码
  const char* results_0_daily_1_high = results_0_daily_1["high"];                                          //当天最高温度
  const char* results_0_daily_1_low = results_0_daily_1["low"];                                            //当天最低温度
  const char* results_0_daily_1_rainfall = results_0_daily_1["rainfall"];                                  //降水
  const char* results_0_daily_1_precip = results_0_daily_1["precip"];                                      //降水概率
  const char* results_0_daily_1_wind_direction = results_0_daily_1["wind_direction"];                      //风向
  const char* results_0_daily_1_wind_direction_degree = results_0_daily_1["wind_direction_degree"];        //风向角度值
  const char* results_0_daily_1_wind_speed = results_0_daily_1["wind_speed"];                              //风速
  const char* results_0_daily_1_wind_scale = results_0_daily_1["wind_scale"];                              //风力等级
  const char* results_0_daily_1_humidity = results_0_daily_1["humidity"];                                  //湿度
  // 通过串口监视器显示以上信息
  String results_0_daily_1_date_str = results_0_daily_1["date"].as<String>();                              //日期
  String results_0_daily_1_text_day_str = results_0_daily_1["text_day"].as<String>();                      //白天天气现象
  int results_0_daily_1_code_day_str = results_0_daily_1["code_day"].as<int>();                            //白天天气现象代码
  String results_0_daily_1_text_night_str = results_0_daily_1["text_night"].as<String>();                  //夜晚天气现象
  int results_0_daily_1_code_night_str = results_0_daily_1["code_night"].as<int>();                        //夜晚天气现象代码
  unsigned int results_0_daily_1_high_str = results_0_daily_1["high"].as<int>();                           //当天最高温度
  unsigned int results_0_daily_1_low_str = results_0_daily_1["low"].as<int>();                             //当天最低温度
  String results_0_daily_1_rainfall_str = results_0_daily_1["rainfall"].as<String>();                      //降水
  String results_0_daily_1_precip_str = results_0_daily_1["precip"].as<String>();                          //降水概率
  String results_0_daily_1_wind_direction_str = results_0_daily_1["wind_direction"].as<String>();          //风向
  int results_0_daily_1_wind_direction_degree_str = results_0_daily_1["wind_direction_degree"].as<int>();  //风向角度值
  String results_0_daily_1_wind_speed_str = results_0_daily_1["wind_speed"].as<String>();                  //风速
  int results_0_daily_1_wind_scale_str = results_0_daily_1["wind_scale"].as<int>();                        //风力等级
  int results_0_daily_1_humidity_str = results_0_daily_1["humidity"].as<int>();                            //湿度

  /*明日天气信息打印方式一*/
  // Serial.print(F("日期："));
  // Serial.println(results_0_daily_1_date_str);
  // Serial.print(F("白天天气现象："));
  // Serial.println(results_0_daily_1_text_day_str);
  // Serial.print(F("白天天气现象代码："));
  // Serial.println(results_0_daily_1_code_day_str);
  // Serial.print(F("夜晚天气现象："));
  // Serial.println(results_0_daily_1_text_night_str);
  // Serial.print(F("夜晚天气现象代码："));
  // Serial.println(results_0_daily_1_code_night_str);
  // Serial.print(F("当天最高温度："));
  // Serial.println(results_0_daily_1_high_str);
  // Serial.print(F("当天最低温度："));
  // Serial.println(results_0_daily_1_low_str);
  // Serial.print(F("降水量："));
  // Serial.println(results_0_daily_1_rainfall_str);
  // Serial.print(F("降水概率："));
  // Serial.println(results_0_daily_1_precip_str);
  // Serial.print(F("风向："));
  // Serial.println(results_0_daily_1_wind_direction_str);
  // Serial.print(F("风向角度值："));
  // Serial.println(results_0_daily_1_wind_direction_degree_str);
  // Serial.print(F("风速："));
  // Serial.println(results_0_daily_1_wind_speed_str);
  // Serial.print(F("风力等级："));
  // Serial.println(results_0_daily_1_wind_scale_str);
  // Serial.print(F("湿度："));
  // Serial.print(results_0_daily_1_humidity_str);

  /*明日天气信息打印方式二*/
  // Serial.printf("日期：%s\n",results_0_daily_1_date_str);
  // Serial.printf("白天天气现象：%s\n",results_0_daily_1_text_day_str);
  // Serial.printf("白天天气现象代码：%d\n",results_0_daily_1_code_day_str);
  // Serial.printf("夜晚天气现象：%s\n",results_0_daily_1_text_night_str);
  // Serial.printf("夜晚天气现象代码：%d\n",results_0_daily_1_code_night_str);
  // Serial.printf("当天最高温度：%d\n",results_0_daily_1_high_str);
  // Serial.printf("当天最低温度：%d\n",results_0_daily_1_low_str);
  // Serial.printf("降水量：%s\n",results_0_daily_1_rainfall_str);
  // Serial.printf("降水概率：%s\n",results_0_daily_1_precip_str);
  // Serial.printf("风向：%s\n",results_0_daily_1_wind_direction_str);
  // Serial.printf("风向角度值：%s\n",results_0_daily_1_wind_direction_degree_str);
  // Serial.printf("风速：%s\n",results_0_daily_1_wind_speed_str);
  // Serial.printf("风力等级：%d\n",results_0_daily_1_wind_scale_str);
  // Serial.printf("湿度：%d\n",results_0_daily_1_humidity_str);

  /********************************************************后天天气信息*****************************************************/
  JsonObject results_0_daily_2 = results_0_daily[2];                                                             
  const char* results_0_daily_2_date = results_0_daily_2["date"];                                          //日期 
  const char* results_0_daily_2_text_day = results_0_daily_2["text_day"];                                  //白天天气现象 
  const char* results_0_daily_2_code_day = results_0_daily_2["code_day"];                                  //白天天气现象代码 
  const char* results_0_daily_2_text_night = results_0_daily_2["text_night"];                              //夜晚天气现象  
  const char* results_0_daily_2_code_night = results_0_daily_2["code_night"];                              //夜晚天气现象代码  
  const char* results_0_daily_2_high = results_0_daily_2["high"];                                          //当天最高温度                                              
  const char* results_0_daily_2_low = results_0_daily_2["low"];                                            //当天最低温度
  const char* results_0_daily_2_rainfall = results_0_daily_2["rainfall"];                                  //降水
  const char* results_0_daily_2_precip = results_0_daily_2["precip"];                                      //降水概率
  const char* results_0_daily_2_wind_direction = results_0_daily_2["wind_direction"];                      //风向
  const char* results_0_daily_2_wind_direction_degree = results_0_daily_2["wind_direction_degree"];        //风向角度值
  const char* results_0_daily_2_wind_speed = results_0_daily_2["wind_speed"];                              //风速
  const char* results_0_daily_2_wind_scale = results_0_daily_2["wind_scale"];                              //风力等级
  const char* results_0_daily_2_humidity = results_0_daily_2["humidity"];                                  //湿度
  const char* results_0_last_update = results_0["last_update"];
  // 通过串口监视器显示以上信息
  String results_0_daily_2_date_str = results_0_daily_2["date"].as<String>();                              //日期
  String results_0_daily_2_text_day_str = results_0_daily_2["text_day"].as<String>();                      //白天天气现象
  int results_0_daily_2_code_day_str = results_0_daily_2["code_day"].as<int>();                            //白天天气现象代码
  String results_0_daily_2_text_night_str = results_0_daily_2["text_night"].as<String>();                  //夜晚天气现象
  int results_0_daily_2_code_night_str = results_0_daily_2["code_night"].as<int>();                        //夜晚天气现象代码
  unsigned int results_0_daily_2_high_str = results_0_daily_2["high"].as<int>();                           //当天最高温度
  unsigned int results_0_daily_2_low_str = results_0_daily_2["low"].as<int>();                             //当天最低温度
  String results_0_daily_2_rainfall_str = results_0_daily_2["rainfall"].as<String>();                      //降水量
  String results_0_daily_2_precip_str = results_0_daily_2["precip"].as<String>();                          //降水概率
  String results_0_daily_2_wind_direction_str = results_0_daily_2["wind_direction"].as<String>();          //风向
  int results_0_daily_2_wind_direction_degree_str = results_0_daily_2["wind_direction_degree"].as<int>();  //风向角度值
  String results_0_daily_2_wind_speed_str = results_0_daily_2["wind_speed"].as<String>();                  //风速
  int results_0_daily_2_wind_scale_str = results_0_daily_2["wind_scale"].as<int>();                        //风力等级
  int results_0_daily_2_humidity_str = results_0_daily_2["humidity"].as<int>();                            //湿度
  /*后天天气信息打印方式一*/
  Serial.print(F("日期："));
  Serial.println(results_0_daily_2_date_str);
  Serial.print(F("白天天气现象："));
  Serial.println(results_0_daily_2_text_day_str);
  Serial.print(F("白天天气现象代码："));
  Serial.println(results_0_daily_2_code_day_str);
  Serial.print(F("夜晚天气现象："));
  Serial.println(results_0_daily_2_text_night_str);
  Serial.print(F("夜晚天气现象代码："));
  Serial.println(results_0_daily_2_code_night_str);
  Serial.print(F("当天最高温度："));
  Serial.println(results_0_daily_2_high_str);
  Serial.print(F("当天最低温度："));
  Serial.println(results_0_daily_2_low_str);
  Serial.print(F("降水量："));
  Serial.println(results_0_daily_2_rainfall_str);
  Serial.print(F("降水概率："));
  Serial.println(results_0_daily_2_precip_str);
  Serial.print(F("风向："));
  Serial.println(results_0_daily_2_wind_direction_str);
  Serial.print(F("风向角度值："));
  Serial.println(results_0_daily_2_wind_direction_degree_str);
  Serial.print(F("风速："));
  Serial.println(results_0_daily_2_wind_speed_str);
  Serial.print(F("风力等级："));
  Serial.println(results_0_daily_2_wind_scale_str);
  Serial.print(F("湿度："));
  Serial.print(results_0_daily_2_humidity_str);

  /*后天天气信息打印方式二*/
  // Serial.printf("日期：%s\n", results_0_daily_2_date_str);
  // Serial.printf("白天天气现象：%s\n", results_0_daily_2_text_day_str);
  // Serial.printf("白天天气现象代码：%d\n", results_0_daily_2_code_day_str);
  // Serial.printf("夜晚天气现象：%s\n", results_0_daily_2_text_night_str);
  // Serial.printf("夜晚天气现象代码：%d\n", results_0_daily_2_code_night_str);
  // Serial.printf("当天最高温度：%d\n", results_0_daily_2_high_str);
  // Serial.printf("当天最低温度：%d\n", results_0_daily_2_low_str);
  // Serial.printf("降水量：%s\n", results_0_daily_2_rainfall_str);
  // Serial.printf("降水概率：%s\n", results_0_daily_2_precip_str);
  // Serial.printf("风向：%s\n", results_0_daily_2_wind_direction_str);
  // Serial.printf("风向角度值：%s\n",results_0_daily_2_wind_direction_degree_str);
  // Serial.printf("风速：%s\n",results_0_daily_2_wind_speed_str);
  // Serial.printf("风力等级：%d\n",results_0_daily_2_wind_scale_str);
  // Serial.printf("湿度：%d\n",results_0_daily_2_humidity_str);
}
